package com.wf.smartcomm.server.weixin.action;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.client.action.BaseAction;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.weixin.util.WeixinUtil;

import net.sf.json.JSONObject;

public class WeiChatUserAction extends BaseAction implements ModelDriven<User>{
	
	private User user = new User();
	
	//HttpSession session;
	
	@Resource
	private UserService userService;
	
	public String show()throws Exception{
		String returnType="user";
		String openId ="";
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
			return "success";
		}else {
			/*Cookie[] cookies = request.getCookies();
			if (cookies != null && cookies.length > 0) {
				for(Cookie cookie : cookies){
		            if(cookie.getName().equals("userId")){
		            	String userId = cookie.getValue();
		            	user = userService.getUserById(userId);
		    			session.setAttribute("user",user);
		    			return "success";
		            }
		        }
			}*/
			String code = request.getParameter("code");
			logger.info("code--------->"+code);
			String oauth_full_url ="";
			if (PushUtils.isDevEnv) {
				oauth_full_url=oauth_base_url_dev+"&code="+code+"&grant_type=authorization_code";
			}else {
				oauth_full_url=oauth_base_url_gen+"&code="+code+"&grant_type=authorization_code";
			}
			JSONObject jsonObject = WeixinUtil.httpRequest(oauth_full_url, "GET", null);
			if (jsonObject != null) {
				logger.info(jsonObject);
				openId = jsonObject.getString("openid");
				logger.info("openId-------->:"+openId);
				user = userService.getUserByOpenId(openId);				
				if (user == null) {
					/*Cookie cookie = new Cookie("openId", openId);
					cookie.setMaxAge(3600*24*30*12*2);
					response.addCookie(cookie);*/
					request.setAttribute("returnType", returnType);
					request.setAttribute("openId", openId);
					return "login";
				}
				/*Cookie cookie = new Cookie("userId", user.getObjectId());
				cookie.setMaxAge(3600*24*30*12*2);
				response.addCookie(cookie);*/
				
				
				session.setAttribute("user", user);
				return "success";
				
			}else {
				logger.info("jsonObject is null");
				request.setAttribute("returnType", returnType);
				request.setAttribute("openId", openId);
				return "login";
			}
		}
	}
	
//	public String logout() {
//		if (session != null) {
//			session.invalidate();
////			session.removeAttribute("userCounts");
////			session.removeAttribute("umCounts");
//		}
//		Cookie[] cookies = request.getCookies();
//        for(int i = 0,len = cookies.length; i < len; i++) {
//            Cookie cookie = new Cookie(cookies[i].getName(),null);
//            cookie.setMaxAge(0);
//            //cookie.setPath("/");
//            response.addCookie(cookie);
//        }
//		return "success";
//	}

	@Override
	public User getModel() {
		return user;
	}

}
